import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:my_meeting/entity/meeting_data.dart';
import 'package:my_meeting/generated/assets.dart';
import 'package:my_meeting/global.dart';
import 'package:my_meeting/routes/router.dart';
import 'package:my_meeting/ui/metting/metting_list/metting_list_logic.dart';
import 'package:my_meeting/utils/utils.dart';

import '../../../generated/l10n.dart';
import '../../../utils/log_utils.dart';
import '../../../widget/dialog/dialog_common_style.dart';

class Metting_listPage extends GetView<Metting_listLogic> {
  const Metting_listPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        title: Text(
          S.current.myMeeting,
          style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
        ),
        actions: [
          GestureDetector(
            onTap: () {
              Get.toNamed(AppRoutes.mettingAll,arguments: controller.meetingList);
            },
            child: Padding(
                padding: EdgeInsets.only(right: 16.w),
                child: Text(S.current.allMeeting,
                    style: TextStyle(
                        color: const Color(0xff1877FF), fontSize: 13.sp))),
          ),
        ],
      ),
      body: Container(
        color: const Color(0xffF8F8F8),
        child: Obx(() {
          return ListView.separated(
              separatorBuilder: (context, index) {
                return Container();
              },
              itemCount: controller.meetingList.length,
              itemBuilder: (context, index) {
                return meetingListWidget(index);
              });
        }),
      ),
      floatingActionButton: InkWell(
        onTap: () {
          Get.toNamed(AppRoutes.meetingEdit)?.then((value) {
            if (value != null) {
              controller.refreshMeetings();
            }
          });
        },
        child: SvgPicture.asset(
          Assets.svgAddMeetting,
          width: 48,
          height: 48,
        ).marginOnly(bottom: 20.h),
      ),
    );
  }

  Widget meetingListWidget(int index) {
    MeetingData data = controller.meetingList[index];
    String state =
        Utils.meetingState(data);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Text(controller.getDisplayTitle(index),
                style: TextStyle(
                    color: const Color(0xff333333),
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600)),
            Text(Utils.weekdayName(data.meetingBeginTime),
                    style: TextStyle(
                        color: const Color(0xff333333).withOpacity(0.6),
                        fontSize: 12.sp,
                        fontWeight: FontWeight.w400))
                .paddingOnly(left: 5.w),
          ],
        ).marginSymmetric(horizontal: 15.w, vertical: 15.h),
        GestureDetector(
          onTap: () {
            Get.toNamed(AppRoutes.mettingDetail,arguments: data)?.then((val){
              if(val != null){
                controller.refreshMeetings();
              }
            });
          },
          child: Container(
            margin: EdgeInsets.only(left: 15.w, right: 15.w),
            padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 15.w),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8.r),
              color: Colors.white,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Container(
                          decoration: BoxDecoration(
                              color: Utils.stateColor(state), shape: BoxShape.circle),
                          width: 6.w,
                          height: 6.w,
                        ),
                        SizedBox(
                          width: 6.w,
                        ),
                        Text(
                          data.meetingName ?? '',
                          style: TextStyle(
                              color: const Color(0xff333333),
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w600),
                        ),
                      ],
                    ),
                    Text(
                      state,
                      style: TextStyle(
                          color: Utils.stateColor(state), fontSize: 12.sp),
                    ),
                  ],
                ),
                const Divider(color: Color.fromRGBO(0, 0, 0, 0.06))
                    .marginSymmetric(vertical: 15.h),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          Utils.getMeetingTime(data.meetingBeginTime),
                          style: TextStyle(
                              color: const Color(0xff333333),
                              fontSize: 16.sp,
                              fontWeight: FontWeight.w700),
                        ),
                        Text(
                          Utils.getMeetingDate(data.meetingBeginTime),
                          style: TextStyle(
                              color: const Color.fromRGBO(51, 51, 51, 0.6),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w500),
                        )
                      ],
                    ),
                    Expanded(
                      child: Row(
                        children: [
                          Expanded(
                            child: Divider(
                              color: const Color.fromRGBO(0, 0, 0, 0.06),
                              endIndent: 5.w,
                              indent: 10.w,
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 10.w),
                            alignment: Alignment.center,
                            height: 18.h,
                            decoration: BoxDecoration(
                                color: const Color.fromRGBO(51, 51, 51, 0.1),
                                borderRadius: BorderRadius.circular(4.r)),
                            child: Text(
                              '${Utils.timeDifference(data.meetingBeginTime!, data.meetingEndTime!)}小时',
                              style: TextStyle(
                                  color: const Color(0xff333333),
                                  fontSize: 10.sp,
                                  fontWeight: FontWeight.w500),
                            ),
                          ),
                          Expanded(
                            child: Divider(
                              color: const Color.fromRGBO(0, 0, 0, 0.06),
                              endIndent: 10.w,
                              indent: 5.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          Utils.getMeetingTime(data.meetingEndTime),
                          style: TextStyle(
                              color: const Color(0xff333333),
                              fontSize: 16.sp,
                              fontWeight: FontWeight.w700),
                        ),
                        Text(
                          Utils.getMeetingDate(data.meetingEndTime),
                          style: TextStyle(
                              color: const Color.fromRGBO(51, 51, 51, 0.6),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w500),
                        )
                      ],
                    ),
                  ],
                ).paddingSymmetric(horizontal: 15.w),
                Row(
                  children: [
                    SvgPicture.asset(
                      Assets.svgLoc,
                      width: 18,
                      height: 18,
                      color: const Color(0xff535353),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 10.w),
                      child: Text(
                        data.meetingAddress ?? '',
                        style: TextStyle(
                            color: const Color(0xff999999),
                            fontSize: 13.sp,
                            fontWeight: FontWeight.w500),
                      ),
                    ),
                  ],
                ).marginOnly(top: 20.h),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SvgPicture.asset(
                      Assets.svgMember,
                      width: 18,
                      height: 18,
                      color: const Color(0xff535353),
                    ),
                    Expanded(
                      child: Text(
                        data.participants ?? '',
                        style: TextStyle(
                            color: const Color(0xff999999),
                            fontSize: 13.sp,
                            fontWeight: FontWeight.w500),
                      ).paddingOnly(left: 10.w),
                    ),
                  ],
                ).marginOnly(top: 10.h),
                if (state !=  S.current.hadEnd && state != S.current.hadCancel)
                  Column(
                    children: [
                      const Divider(color: Color.fromRGBO(0, 0, 0, 0.06))
                          .marginOnly(top: 10.h, bottom: 10.h),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          InkWell(
                            onTap: (){
                              LogD("邀请参会人");
                            },
                            child: Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(
                                  horizontal: 10.w, vertical: 3.h),
                              decoration: BoxDecoration(
                                  color: const Color(0xff1877FF),
                                  border: Border.all(color: const Color(0xff1877FF)),
                                  borderRadius: BorderRadius.circular(37.r)),
                              child: Text(
                                S.current.invitePerson,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 13.sp,
                                    fontWeight: FontWeight.w500),
                              ),
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              Get.dialog(CommonDialog(
                                title: S.current.cancelMeetingTitle,
                                content: S.current.cancelMeetingContent,
                                posiTap: () {
                                  data.meetingState = 1;
                                  global.updateMeeting(data).then((value){
                                    controller.meetingList[index].meetingState = 1;
                                    controller.meetingList.refresh();
                                  });
                                },
                              ));
                            },
                            child: Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(
                                  horizontal: 10.w, vertical: 3.h),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  border:
                                  Border.all(color: const Color(0xff1877FF)),
                                  borderRadius: BorderRadius.circular(37.r)),
                              child: Text(
                                S.current.cancelMeeting,
                                style: TextStyle(
                                    color: const Color(0xff1877FF),
                                    fontSize: 13.sp,
                                    fontWeight: FontWeight.w500),
                              ),
                            ).paddingOnly(left: 10.w),
                          )
                        ],
                      )
                    ],
                  )
              ],
            ),
          ),
        )
      ],
    );
  }
}
